package com.wedistudio.slides.search.slideshare;

import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.google.resting.Resting;
import com.google.resting.component.RequestParams;
import com.google.resting.component.impl.BasicRequestParams;
import com.google.resting.component.impl.ServiceResponse;
import com.google.resting.component.impl.xml.XMLAlias;
import com.wedistudio.slides.search.Author;
import com.wedistudio.slides.search.Content;
import com.wedistudio.slides.search.ContentHeader;
import com.wedistudio.slides.search.Language;
import com.wedistudio.slides.search.OrderBy;
import com.wedistudio.slides.search.Search;
import com.wedistudio.slides.search.Source;

public class SlideShareSource implements Source {

	private static String APIKEY = "E6F5XDlR";
	private static String SHARED_SECRET = "cXSRZ8E0";

	private static String API_URL_BASE = "https://www.slideshare.net/api/2/";
	private static String GET_SLIDESHOW_INFORMATION = "get_slideshow";
	private static String GET_SLIDESHOW_BY_TAG = "get_slideshows_by_tag";
	private static String GET_SLIDESHOW_BY_GROUP = "get_slideshows_by_group";
	private static String GET_SLIDESHOW_BY_USER = "get_slideshows_by_user";
	private static String SEARCH_SLIDESHOW = "search_slideshows";
	private static String GET_USER_GROUPS = "get_user_groups";
	private static String GET_USER_FAVORITES = "get_user_favorites";
	private static String GET_USER_CONTACTS = "get_user_contacts";
	private static String GET_USER_TAGS = "get_user_tags";

	@Override
	public String getSource() {
		return "slideshare";
	}

	private String orderby(OrderBy order) {
		switch (order) {
		case RELEVENCE:
			return "relevance";
		case LAST_PUBLISHED:
			return "latest";
		case POPULARITY:
			return "mostviewed";
		}
		return null;
	}

	private String lang(Language language) {
		switch (language) {
		case ANY:
			return "**";
		case ARABIC:
			return "!!";
		case CHINESE:
			return "zh*";
		case DUTCH:
			return "nl";
		case ENGLISH:
			return "en";
		case FRENCH:
			return "fr";
		case GERMAN:
			return "de";
		case ITALIAN:
			return "it";
		case JAPANESE:
			return "ja";
		case KOREAN:
			return "ko";
		case OTHER:
			return "!!";
		case PORTUGUESE:
			return "pt";
		case ROMANIAN:
			return "ro";
		case SPANISH:
			return "**";

		}

		return "**";
	}
	
	private static RequestParams getParams() {
		RequestParams params = new BasicRequestParams();
		Date now = new Date();
		String ts = Long.toString(now.getTime() / 1000);
		String hash = new String(Hex.encodeHex(DigestUtils.sha(SHARED_SECRET
				+ ts))).toLowerCase();
		params.add("ts", ts);
		params.add("hash", hash);
		params.add("api_key", APIKEY);
		return params;
	}
 
	@Override
	public List<ContentHeader> getContents(Search criteria) {
		RequestParams params = getParams();
		params.add("q", criteria.getKeywords());
		params.add("page", ""
				+ (1 + criteria.getOffset() / criteria.getLimit()));
		params.add("items_per_page", "" + criteria.getLimit());
		params.add("sort", orderby(criteria.getOrderby()));
		params.add("lang", lang(criteria.getLanguage()));
		ServiceResponse resp = Resting.get(API_URL_BASE + SEARCH_SLIDESHOW,
				443, params);
		String xml = resp.getContentData().getContentInString();
		return null;
	}

	@Override
	public List<ContentHeader> getRelatedContents(ContentHeader content) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ContentHeader> getContents(Author author) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Content complete(ContentHeader content) {
		// TODO Auto-generated method stub
		return null;
	}
}
